package com.mall507.fivepeaks.activity.community;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xrefresh.XSwipeRefreshLayout;
import com.mall507.comn.model.Message;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;
import com.mall507.fivepeaks.widget.xdialog.XAddressDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 协会详情 | 活动
 * @date 2019/8/8
 */
@Route(path = "/community/CommunityAssociationInfoActivityRUI")
public class CommunityAssociationInfoActivityRUI extends AbsRecyclerViewUI<Message> {
    private List<String> listUrl;
    private View headView;
    @ViewInject(R.id.collection) //收藏
    LinearLayout collection;
    @ViewInject(R.id.img_collection) //收藏图片
    ImageView img_collection;
    @ViewInject(R.id.lickNumber)
    TextView lickNumber;
    @ViewInject(R.id.send) //发送评论
    TextView send;
    @ViewInject(R.id.comment) //评论内容
    EditText comment;
    @ViewInject(R.id.swipeRefresh)
    XSwipeRefreshLayout swipeRefresh;
    
    TextView shijian;
    TextView dizhi;
    TextView renshu;
    private int flag = 0;
    private int flag2 = 0;
    private int flag3 = 0;

    private int page = 0;
    
    String associationAddress = ""; // 地址
//    String associationlat = ""; // 地址
//    String associationlng = ""; // 地址
    
    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_community_association_info_activity;
    }

    @Override
    protected String setHttpPath() {
        return "Comment/getAssociationComment";
    }


    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("详情");
        showLoding();
        lickNumber.setText(getIntent().getStringExtra("collection_count"));
        headView = getLayoutInflater().inflate(R.layout.head_rv_community_association_info_activity, null);
        getAdapter().setHeaderView(headView);
        shijian = headView.findViewById(R.id.time);
        dizhi = headView.findViewById(R.id.address);
        renshu = headView.findViewById(R.id.number);
        views();
        getComment();
        isCollection();
        collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserTools.getInstance().getIsLogin()){
                    if (page == 0){
                        setCollection();
                    }else if (page == 1){
                        cancelCollection();
                    }
                }else {
                    alert("请登录之后尝试");
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
        
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserTools.getInstance().getIsLogin()){
                    if (TextUtils.isEmpty(comment.getText())){
                        alert("评论内容不能为空");
                    }else {
                        sendComment();
                    }
                }else {
                    alert("请登录之后尝试");
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
        shijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag == 0){
                    shijian.setMaxLines(100);
                    flag = 1;
                } else if (flag == 1) {
                    shijian.setMaxLines(1);
                    flag = 0;
                }
            }
        });
        dizhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new XAddressDialog().show(CommunityAssociationInfoActivityRUI.this, associationAddress,"","",new XAddressDialog.XUserMainSignRewardDialogCallBack() {
                    @Override
                    public void confirm() {
                        
                    }

                    @Override
                    public void cancel() {

                    }
                });
            }
        });
        renshu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag3 == 0){
                    renshu.setMaxLines(100);
                    flag3 = 1;
                } else if (flag3 == 1) {
                    renshu.setMaxLines(1);
                    flag3 = 0;
                }
            }
        });
    }
    

    @Override
    protected List<Message> handleHttpData(JSONObject response) {
        List<Message> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray data = response.optJSONArray("data");
            if (data != null && data.length() > 0) {
                for (int i = 0; i < data.length(); i++) {
                    JSONObject object = data.optJSONObject(i);
                    JSONObject member = data.optJSONObject(i).optJSONObject("member");
                    Message model = new Message();
                    model.setNid(object.optString("id"));
                    if (member != null && member.length()>0){
                        model.setHead_img(member.optString("head_img"));
                        model.setNickname(member.optString("nickname"));
                    }
                    model.setContent(object.optString("content"));
                    model.setCreate_at(object.optString("create_at"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Association/getAssociationActivity", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                getTitleView().setContent(data.optString("title"));
                ((TextView)headView.findViewById(R.id.time)).setText(data.optString("activity_time")); //活动时间
                associationAddress = data.optString("activity_address"); //活动地址
                ((TextView)headView.findViewById(R.id.address)).setText(data.optString("activity_address")); //活动地址地址
                ((TextView)headView.findViewById(R.id.number)).setText(data.optString("activity_number") + "人"); //活动参与人数
                ((TextView)headView.findViewById(R.id.content)).setText(data.optString("desc")); //活动参与人数
                ((ExpandImageView)headView.findViewById(R.id.codeImage)).setImageURI(data.optString("activity_wx_qr")); //活动参与二维码
                ((TextView)headView.findViewById(R.id.codeInfo)).setText(data.optString("activity_remarks")); //活动参与备注
                ((TextView)headView.findViewById(R.id.start)).setText("开始时间：" + data.optString("activity_start")); //活动开始时间
                ((TextView)headView.findViewById(R.id.end)).setText("截至时间：" + data.optString("activity_end")); //活动结束时间
                lickNumber.setText(data.optString("collection_count")); //文章收藏数量
                JSONArray pics=data.optJSONArray("imgs");
                List<String> listUrl=new ArrayList<>();
                for(int i=0;i<pics.length();i++){
                    listUrl.add(pics.optString(i));
                }
                bindTopBannerData(listUrl);
                dismissLoding();
            }

            @Override
            public void onFail() {
                super.onFail();
                showLodingFail();
            }
        });
    }

    //banner数据绑定
    private void bindTopBannerData(List<String> listUrl) {
        BGABanner banner = getAdapter().getHeaderView().findViewById(R.id.head_banner);
        banner.setAdapter(new BGABanner.Adapter<View, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, String url, int position) {
                SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.sdv_item_fresco_content);
                simpleDraweeView.setImageURI(url);
            }
        });
        
        banner.setData(R.layout.homerecycle_top_banner_content, listUrl, null);
    }
    
    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_tour_item_info;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, Message model) {
        holder.setText(R.id.name, model.getNickname());
        holder.setText(R.id.desc, model.getContent());
        holder.setText(R.id.time, model.getCreate_at());
        ((CircleImageView) holder.getView(R.id.icon)).setImageURI(model.getHead_img());
    }

    private void views(){
        BaseHttp.getInstance().query("Action/viewBlog", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
            }
        });
    }

    private void isCollection(){
        BaseHttp.getInstance().query("Action/isCollection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("data") == 1){
                    page = 1;
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_blue));
                }else {
                    page = 0;
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_hui));
                }
                super.onSuccess(response);
            }
        });
    }

    private void setCollection(){
        BaseHttp.getInstance().query("Action/collection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    collectionput();
                }else {
                    alert(response.optString("msg"));
                }
                super.onSuccess(response);
            }
        });
    }

    private void cancelCollection(){
        BaseHttp.getInstance().query("Action/cancelCollection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    collectionput();
                }else {
                    alert(response.optString("msg"));
                }
                super.onSuccess(response);
            }
        });
    }

    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    private void sendComment(){
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
            params.put("id", getIntent().getStringExtra("id"));
            params.put("content", comment.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("comment/subProductComment", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    comment.getText().clear();
                    onRefresh();
                    alert(response.optString("msg"));
                }else {
                    alert(response.optString("msg"));
                }
                super.onSuccess(response);
            }
        });
    }

    private void collectionput(){
        final int collection_count = Integer.parseInt(lickNumber.getText().toString());
        img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_blue));
        if (page == 0){
            lickNumber.setText(String.valueOf(collection_count + 1));
            page = 1;
        }else if (page == 1){
            img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_hui));
            lickNumber.setText(String.valueOf(collection_count - 1));
            page = 0;
        }
    }
}
